package com.podmanager.UI;

import com.podmanager.data.*;
import com.podmanager.search.SearchPodcastsAsyncTask;

import android.os.Bundle;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class AddSubscription extends ListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_subscription);

		OnClickListener urlClickListener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				Bundle bundle = new Bundle();

				//get URL string, make text to notify user that subscription is starting
				EditText et = ((EditText) findViewById(R.id.add_url_text));
				String url = et.getText().toString();
	        	Toast.makeText(v.getContext(), "attempting to subscribe", Toast.LENGTH_SHORT).show();

				if (url==null || url.equals(""))
				{
					// 1. Instantiate an AlertDialog.Builder with its constructor
					AlertDialog.Builder builder = new AlertDialog.Builder(AddSubscription.this);

					// 2. Chain together various setter methods to set the dialog characteristics
					builder.setMessage("You must enter a valid URL in the URL Text Field")
					       .setTitle("No URL Entered")
					       .setPositiveButton("OK", null)
					       .show();
					return;
				}
				if (!PodManager.isOnline(AddSubscription.this))
				{
					// 1. Instantiate an AlertDialog.Builder with its constructor
					AlertDialog.Builder builder = new AlertDialog.Builder(AddSubscription.this);

					// 2. Chain together various setter methods to set the dialog characteristics
					builder.setMessage("Sorry, that feature isn't available when you aren't connected to the internet")
					       .setTitle("No connection")
					       .setPositiveButton("OK", null)
					       .show();
					return;
				}
	        	//begin background task to process the URL
				new ProcessFeedAsyncTask(v.getContext()).execute(url);
				
				intent.putExtras(bundle);
				setResult(RESULT_OK, intent);
				
			}
		};
		OnClickListener searchClickListener= new OnClickListener() {
			@Override
			public void onClick(View v) {
				EditText et = (EditText)findViewById(R.id.add_search_text);
				String search = et.getText().toString();
				if (search==null || search.equals(""))
				{
					// 1. Instantiate an AlertDialog.Builder with its constructor
					AlertDialog.Builder builder = new AlertDialog.Builder(AddSubscription.this);

					// 2. Chain together various setter methods to set the dialog characteristics
					builder.setMessage("You must enter something you want to search for")
					       .setTitle("No search entered")
					       .setPositiveButton("OK", null)
					       .show();
					return;
				}
				if (!PodManager.isOnline(AddSubscription.this))
				{
					// 1. Instantiate an AlertDialog.Builder with its constructor
					AlertDialog.Builder builder = new AlertDialog.Builder(AddSubscription.this);

					// 2. Chain together various setter methods to set the dialog characteristics
					builder.setMessage("Sorry, that feature isn't available when you aren't connected to the internet")
					       .setTitle("No connection")
					       .setPositiveButton("OK", null)
					       .show();
					return;
				}

				new SearchPodcastsAsyncTask(null, AddSubscription.this).execute(search);
				InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
	            imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS);
			}
		};
		 

		Button urlButton = ((Button)this.findViewById(R.id.add_url_button));
		urlButton.setOnClickListener(urlClickListener);
		Button searchButton = ((Button)this.findViewById(R.id.add_search_button));
		searchButton.setOnClickListener(searchClickListener);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long list_RowID) {
    	String url = ((TextView)v.findViewById(R.id.add_search_item_URL)).getText().toString();
    	String title = ((TextView)v.findViewById(R.id.add_search_item_title)).getText().toString();
		// 1. Instantiate an AlertDialog.Builder with its constructor
		AlertDialog.Builder builder = new AlertDialog.Builder(AddSubscription.this);

		// 2. Chain together various setter methods to set the dialog characteristics
		builder.setMessage("Do you want to subscribe to '"+title+"'?")
		       .setTitle("Subscribe?")
		       .setPositiveButton("OK", new DOCL(url))
		       .setNegativeButton("Cancel", null)
		       .show();
    }
    private class DOCL implements DialogInterface.OnClickListener
    {
		private String mUrl;
		public DOCL(String url){mUrl=url;}
		@Override
		public void onClick(DialogInterface dialog, int which) {
			new ProcessFeedAsyncTask(AddSubscription.this).execute(mUrl);

			setResult(RESULT_OK, null);
		}
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
//        getMenuInflater().inflate(R.menu.activity_add_subscription, menu);
        return true;
    }
}
